package com.yanqu.road.server.manger.union;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUser;
import com.yanqu.road.logic.bussiness.union.activitygroup.UnionActivityGroupBusiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.UnionActivityGroupModule;
import com.yanqu.road.server.manger.activity.ActivityInfoMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.ServerType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GameUnionActivityGroupMgr extends BaseUnionActivityGroupMgr {

    public static int getGroupId(int activityId, String unionUid) {
        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            return getGroupId(activityInfo, unionUid);
        }
        return UnionActivityGroupHelper.NO_GROUP_ID;
    }

    public static int getGroupId(int activityId, long userId) {
        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            return getGroupId(activityInfo, userId);
        }
        return UnionActivityGroupHelper.NO_GROUP_ID;
    }

    public static int getViewDataGroupId(int activityId, long userId){
        int groupId = getGroupId(activityId, userId);
        if(groupId != UnionActivityGroupHelper.NO_GROUP_ID ){
            return groupId;
        }
        ActivityInfo activityInfo = ActivityInfoMgr.getActivityInfo(activityId);
        if(activityInfo != null){
            if(UnionActivityGroupHelper.needGroup(activityInfo)) {
                groupId = UnionActivityGroupHelper.NEED_GROUP_FIRST_ID;
            }else {
                groupId = UnionActivityGroupHelper.NO_NEED_GROUP_ID;
            }
        }
        return groupId;
    }

    public static UnionActivityGroup getDbGroupData(int activityId, long userId) {
        if (!activityGroupMap.containsKey(activityId)) {
            Map<Integer, UnionActivityGroup> map = UnionActivityGroupBusiness.getUnionActivityGroupMap(activityId, GameServer.getInstance().getServerId());
            if (map != null && !map.isEmpty()) {
                List<UnionActivityGroupUnion> unionList = UnionActivityGroupBusiness.getUnionActivityGroupUnionList(activityId, new ArrayList<>(map.keySet()));
                for (UnionActivityGroupUnion union : unionList) {
                    UnionActivityGroup group = map.get(union.getGroupId());
                    if (group != null) {
                        group.getUnionDataMap().put(union.getUnionUid(), union);
                    }
                }
            }
            putGroupMap(activityId, map);
        }
        int groupId = getGroupId(activityId, userId);
        return getGroupData(activityId, groupId);
    }

    public static void allSyncAble() {
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(UnionActivityGroupModule.class).syncAble();
        }
    }

    /**
     * 获取活动中的商会id,不是商会分组活动就取玩家身上的
     * @param activityInfo
     * @param player
     * @return
     */
    public static String getActivityUnionUid(ActivityInfo activityInfo, GamePlayer player) {
        if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
            return BaseUnionActivityGroupMgr.getUnionUid(activityInfo.getActivityId(), player.getUserId());
        }
        return player.getUserInfo().getUnionUid();
    }

    /**
     * 获得活动中的商会成员,有分组要取分组锁定时的成员
     * @param activityId
     * @param unionUid
     * @return
     */
    public static List<Long> getActivityUnionMemberIds(int activityId, String unionUid) {
        List<Long> memberIds = new ArrayList<>();
        UnionActivityGroupUnion unionData = GameUnionActivityGroupMgr.getUnionData(activityId, unionUid);
        if (unionData == null) {
            memberIds = CrossUnionMgr.getUnionMemberIdList(unionUid);
        } else {
            for (UnionActivityGroupUser groupUser : unionData.getUserMap().values()) {
                memberIds.add(groupUser.getUserId());
            }
        }
        return memberIds;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        loadDbData(ServerType.SERVER_GAME, GameServer.getInstance().getServerId());
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }
}
